package com.example.learn.controller;

import com.example.learn.domain.IncomeChart;
import com.example.learn.entity.Expenditure;
import com.example.learn.entity.Income;
import com.example.learn.entity.User;
import com.example.learn.service.ExpenditureService;
import com.example.learn.service.IncomeService;
import com.example.learn.token.CurrentUser;
import com.example.learn.token.UserLoginToken;
import com.example.learn.utile.Response.BaseResponse;
import com.example.learn.utile.Response.ObjectRestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/api/expenditure")
public class ExpenditureController {
    @Autowired
    ExpenditureService expenditureService;

    @UserLoginToken
    @RequestMapping(value = "getexpenditure",method = RequestMethod.GET)
    public BaseResponse getexpenditure(@RequestParam String queryTime,@RequestParam String queryname, @CurrentUser User user){


        List<Expenditure> inexpenditure;
        if(queryTime.isEmpty()){
            LocalDate today = LocalDate.now();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
            queryTime=dateTimeFormatter2.format(today);
        }
                inexpenditure=      expenditureService.getexpenditure(queryTime,queryname,user);
        return new ObjectRestResponse().data(inexpenditure);
    }

    @UserLoginToken
    @RequestMapping(value = "addexpenditure",method = RequestMethod.POST)
    public BaseResponse addexpenditure(@RequestBody Expenditure expenditure , @CurrentUser User user){
        String incomeStr=      expenditureService.addexpenditure(expenditure,user);
        return new ObjectRestResponse().data(incomeStr);
    }

    @RequestMapping(value = "deleteexpenditure",method = RequestMethod.POST)
    public BaseResponse deleteexpenditure(String id){
        String incomeStr=      expenditureService.deleteexpenditure(id);
        return new ObjectRestResponse().data(incomeStr);
    }
    @UserLoginToken
    @RequestMapping(value = "topleftmethod",method = RequestMethod.POST)
    public BaseResponse topleftmethod(@CurrentUser User user, @RequestParam String queryTime, @RequestParam String queryname){
        List<IncomeChart> incomeList;
        if(queryTime.isEmpty()){
            LocalDate today = LocalDate.now();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
            queryTime=dateTimeFormatter2.format(today);
        }
        incomeList=    expenditureService.topleftmethod(queryTime,queryname,user);

        return new ObjectRestResponse().data(incomeList);
    }
    @UserLoginToken
    @RequestMapping(value = "downrightmethod",method = RequestMethod.POST)
    public BaseResponse downrightmethod(@CurrentUser User user,  @RequestParam String queryTime,  @RequestParam String queryname){
        List<String> stringList;
        if(queryTime.isEmpty()){
            LocalDate today = LocalDate.now();
            DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM");
            queryTime=dateTimeFormatter2.format(today);
        }
        stringList=    expenditureService.downrightmethod(user,queryname,queryTime);

        return new ObjectRestResponse().data(stringList);
    }
}
